package com.ecnu.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.ProjectExcellent;
import com.ecnu.system.domain.ProjectResult;
import com.ecnu.system.domain.dto.ExcellentDTO;
import com.ecnu.system.domain.dto.ProExcellentDTO;
import com.ecnu.system.domain.dto.ProjectListDTO;
import com.ecnu.system.service.IProjectExcellentService;
import com.ecnu.system.service.IProjectResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "活动推优")
@RestController
@RequestMapping("/project/Excellent")
public class ProjectExcellentController extends BaseController {
    @Autowired
    private IProjectExcellentService projectExcellentService;
    @Autowired
    private IProjectResultService projectResultService;

    /**
    * 前台接口
    */

    /**
     * 首页优秀作品
     */
    @GetMapping("/homeList")
    @ApiOperation(value = "首页优秀作品")
    public TableDataInfo homeList(String proType){
        return projectExcellentService.homeList(proType);
    }

//    /**
//     * 项目优秀作品
//     */
//    @PostMapping("/projectList")
//    @ApiOperation(value = "项目优秀作品（待合并）")
//    public TableDataInfo projectList(@Valid @RequestBody ProjectListDTO dto){
//        return projectExcellentService.list(dto);
//    }


    /**
     * 活动优秀作品
     */
    @PostMapping("/list")
    @ApiOperation(value = "前台活动中的优秀作品")
    public TableDataInfo list(@Valid @RequestBody ExcellentDTO dto){
        LoginUser loginUser = new LoginUser();
//        LoginUser loginUser = getLoginUser();
        return projectExcellentService.projectList(dto,loginUser);
    }




    /**
     * 后台接口
     */
    @PostMapping("/save")
    @ApiOperation(value = "推优")
    @PreAuthorize("@ss.hasPermi('project:Excellent:save')")
    public AjaxResult save(@Valid @RequestBody ProExcellentDTO dto) {
        //判断成果说否存在
        LambdaQueryWrapper<ProjectResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectResult::getId, dto.getResultId());
        ProjectResult result = projectResultService.getOne(wrapper);
        if (result == null) {
            return AjaxResult.error("成果不存在，请刷新页面");
        }
        //判断是否已推优
        long count = projectExcellentService.count(new LambdaQueryWrapper<ProjectExcellent>()
                .eq(ProjectExcellent::getResultId, dto.getResultId()));
        if (count > 0) {
            return AjaxResult.success("该成果已进行推优，不可重复推优");
        }

        //添加推优
        ProjectExcellent excellent = new ProjectExcellent();
        BeanUtils.copyProperties(dto, excellent);
        boolean save = projectExcellentService.save(excellent);
        return AjaxResult.success("推优成功", save);
    }

    @PostMapping("/update")
    @ApiOperation(value = "取消推优")
    @PreAuthorize("@ss.hasPermi('project:Excellent:update')")
    public AjaxResult update(@Valid @RequestBody ProExcellentDTO resultId) {
        projectExcellentService.del(resultId.getResultId());

        return AjaxResult.success("撤销成功");
    }
}
